package cn.sjtu.data_structure.linked_list;

/**
 * @author syo 2319615164@qq.com
 * @description 单链表类
 * @date 2024/5/12
 * @Copyright syo
 */
public class LinkedList<E> {
    public LinkedNode<E> head = null;
    public int size = 0;
    public LinkedList() {}

    // 尾插
    public void addToTail(E e) {
        LinkedNode<E> newNode = new LinkedNode<E>(e, null);
        size++;
        if (head == null) {
            head = newNode;
            return;
        }

        LinkedNode<E> cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = newNode;
    }

    // 头插
    public void addToHead(E e) {
        head = new LinkedNode<E>(e, head);
        size++;
    }

    // 在第i个位置后插入
    public void insert(E e, int index) {
        if (index <= 0 || index > size) {
            throw new IndexOutOfBoundsException();
        }

        LinkedNode<E> newNode = new LinkedNode<E>(e, null);
        LinkedNode<E> cur = head;
        for (int i = 1; i < index; i++) {
            cur = cur.next;
        }

        newNode.next = cur.next;
        cur.next = newNode;
        size++;
    }

    // 删除头节点
    public LinkedNode<E> deleteHead() {
        if (head == null) {
            throw new IndexOutOfBoundsException();
        }
        LinkedNode<E> result = head;
        head = head.next;
        size--;
        return result;
    }

    // 删除尾节点
    public LinkedNode<E> deleteTail() {
        if (head == null) {
            throw new IndexOutOfBoundsException();
        }

        LinkedNode<E> dummyNode = new LinkedNode<E>(null, head);

        LinkedNode<E> cur = dummyNode;
        while (cur.next.next != null) {
            cur = cur.next;
        }
        LinkedNode<E> result = cur.next;
        cur.next = cur.next.next;
        head = dummyNode.next;
        size--;
        return result;
    }

    // 删除第i个位置上的元素
    public void delete(int index) {
        if (index <= 0 || index > size) {
            throw new IndexOutOfBoundsException();
        }

        LinkedNode<E> dummyNode = new LinkedNode<E>(null, head);
        LinkedNode<E> cur = dummyNode;
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        cur.next = cur.next.next;
        head = dummyNode.next;
        size--;
    }

    // 获取第i个位置上的元素值
    public E get(int index) {
        if (index <= 0 || index > size) {
            throw new IndexOutOfBoundsException();
        }

        LinkedNode<E> cur = head;
        for (int i = 1; i < index; i++) {
            cur = cur.next;
        }
        return cur.val;
    }

    // 修改第i个位置上的元素值
    public void set(int index, E e) {
        if (index <= 0 || index > size) {
            throw new IndexOutOfBoundsException();
        }

        LinkedNode<E> cur = head;
        for (int i = 1; i < index; i++) {
            cur = cur.next;
        }
        cur.val = e;
    }

    // 判断链表是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    // 判断某个元素是否在其中
    public boolean contains(E e) {
        for (LinkedNode<E> cur = head; cur != null; cur = cur.next) {
            if (cur.val.equals(e)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (LinkedNode<E> cur = head; cur != null; cur = cur.next) {
            sb.append(cur.val);
            if (cur.next != null) {
                sb.append(", ");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<String>();
        linkedList.addToHead("a");
        linkedList.addToHead("b");
        linkedList.addToHead("c");
        linkedList.addToTail("d");
        linkedList.addToTail("e");
        linkedList.deleteHead();
        linkedList.deleteTail();
        System.out.println(linkedList.toString());
        System.out.println(linkedList.size);
        System.out.println(linkedList.isEmpty());
        System.out.println(linkedList.contains("d"));
    }
}

